package updateBook;

import libraryRun.MainFrame;
import mySQLServer.Link;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateBook extends JFrame {
    private UpdateBook myUpdateBook;
    public UpdateBook(){
        super("图书修改界面");
        myUpdateBook=this;
    }

    public void updateBook(MainFrame myJFrame){
        setLayout(null);
        Container container=this.getContentPane();
        setSize(1200,650);

        //面板
        JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER));
        JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER,70,40));

        //文本行
        JTextField field1=new JTextField(15);
        JTextField field2=new JTextField(15);
        JTextField field3=new JTextField(15);
        JTextField field4=new JTextField(15);

        //按钮
        JButton button1=new JButton("修 改");
        JButton button2=new JButton("取 消");

        //监听器
        ImageIcon imageIcon1=new ImageIcon("icon/imageIcon6.jpg");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(50,45,Image.SCALE_AREA_AVERAGING));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(myUpdateBook,"确定修改吗？","确认修改",JOptionPane.YES_NO_CANCEL_OPTION,1,imageIcon1)==0){
                    if(field1.getText().equals("")||field2.getText().equals("")||field3.getText().equals("")||field4.getText().equals("")){
                        JOptionPane.showMessageDialog(myUpdateBook,"请输入完整图书信息","输入确认",1,imageIcon1);
                        return;
                    }
                    Link link=new Link();
                    try {
                        Connection con=link.linkDatebase();
                        String isSqlExisted="Select * from [books] where bookId="+field2.getText()+"and bookName='"+field1.getText()+"'";
                        String sql="update [books] set price="+field3.getText()+",booksCount="+field4.getText()+" Where bookId="+field2.getText();
                        int is=updateIntoSQLServer(con,isSqlExisted,sql,field2.getText(),field4.getText());
                        if(is==0){
                            JOptionPane.showMessageDialog(myUpdateBook,"该书籍不存在！","信息确认",1,imageIcon1);
                        }
                        else if(is==1){
                            JOptionPane.showMessageDialog(myUpdateBook,"修改成功！","确认修改",1,imageIcon1);
                        }
                        else
                            JOptionPane.showMessageDialog(myUpdateBook,"无法修改，剩余书籍:"+is+"本，修改后书籍数量少于剩余数量！","信息提示",1,imageIcon1);
                        link.closeDatebase(con);
                    } catch (ClassNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (SQLException exception) {
                        exception.printStackTrace();
                    }
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myJFrame.setVisible(true);
                dispose();
            }
        });

        myUpdateBook.addWindowListener(new WindowListener() {

            public void windowOpened(WindowEvent e) {

            }

            public void windowClosing(WindowEvent e) {
                myJFrame.setVisible(true);
            }

            public void windowClosed(WindowEvent e) {

            }

            public void windowIconified(WindowEvent e) {

            }

            public void windowDeiconified(WindowEvent e) {

            }

            public void windowActivated(WindowEvent e) {

            }

            public void windowDeactivated(WindowEvent e) {

            }
        });

        //标签
        JLabel label=new JLabel("图 书 修 改");
        JLabel label1=new JLabel("图 书 名");
        JLabel label2=new JLabel("图 书 Id");
        JLabel label3=new JLabel("修 改 价 格");
        JLabel label4=new JLabel("修 改 数 量");


        //字体
        Font font1=new Font("宋体",Font.BOLD,45);
        Font font2=new Font("宋体",Font.BOLD,30);
        Font font3=new Font("宋体",Font.BOLD,17);

        //设置字体
        label.setFont(font1);
        label1.setFont(font2);
        label2.setFont(font2);
        label3.setFont(font2);
        label4.setFont(font2);
        field1.setFont(font3);
        field2.setFont(font3);
        field3.setFont(font3);
        field4.setFont(font3);

        //设置位置
        p1.setBounds(220,20,750,120);
        p2.setBounds(310,160,550,350);

        //设置大小
        label.setPreferredSize(new Dimension(700,120));
        label1.setPreferredSize(new Dimension(180,30));
        label2.setPreferredSize(new Dimension(180,30));
        label3.setPreferredSize(new Dimension(180,30));
        label4.setPreferredSize(new Dimension(180,30));
        button1.setPreferredSize(new Dimension(100,30));
        button2.setPreferredSize(new Dimension(100,30));

        //设置对其方式
        label.setHorizontalAlignment(JLabel.CENTER);

        //放入面板
        p1.add(label);

        p2.add(label1);
        p2.add(field1);
        p2.add(label2);
        p2.add(field2);
        p2.add(label3);
        p2.add(field3);
        p2.add(label4);
        p2.add(field4);
        p2.add(button1);
        p2.add(button2);

        //放入容器
        container.add(p1);
        container.add(p2);

        setIconImage((new ImageIcon("icon/imageIcon5.jpg")).getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    public int updateIntoSQLServer (Connection con,String isSqlExisted,String sql,String Id,String booksCount) throws SQLException,ClassNotFoundException{
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery(isSqlExisted);
        int isExusted=0;
        int increaseCount=0,reaminedCount=0;
        while (rs.next()){
            if(Integer.parseInt(booksCount)<rs.getInt("remainedCount"))
                return rs.getInt("remainedCount");
//            System.out.println(Integer.parseInt(booksCount)+","+rs.getInt("booksCount"));
            if(Integer.parseInt(booksCount)>rs.getInt("booksCount")){
                increaseCount=Integer.parseInt(booksCount)-rs.getInt("booksCount");
                reaminedCount=rs.getInt("remainedCount")+increaseCount;
            }
            else{
                increaseCount=rs.getInt("booksCount")-Integer.parseInt(booksCount);
                reaminedCount=rs.getInt("remainedCount")-increaseCount;
            }
//            System.out.println(reaminedCount);
            isExusted=1;
        }
        if (isExusted==1){
            stmt.executeUpdate(sql);
            sql="update [books] set remainedCount="+reaminedCount+" where bookId="+Id;
            stmt.executeUpdate(sql);
            return 1;
        }
        return 0;
    }

}
